<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/itusurveyplugin/lib.php');
require_once($CFG->dirroot . "/local/itusurveyplugin/forms.php");

//Require authentication and authorization
require_login($SITE);
require_capability('local/itusurveyplugin:manageitusurvey', context_system::instance());

//prepare moodle_url
global $DB;
$path = $CFG->wwwroot.'/local/itusurveyplugin/itusurvey/edit_survey.php';

$survey_id = required_param('id', PARAM_TEXT);
/* Chuan bi trang web can hien thi */	
$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('Edit ITU Survey');
$PAGE->set_heading('Edit ITU Survey');
$PAGE->set_pagelayout('standard');

navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

echo $OUTPUT->header();
//Include functions
$mylib = new survey_management();
//New form instance
$mform = new form_edit_survey(null, array("id"=>$survey_id));
$mform->display();
//cancel action
if ($mform->is_cancelled()) {

echo 'hủy thao tác';

}//submit action 
else if ($fromform = $mform->get_data()) 
{	
    $fromform = $mform->get_data();
    $old_survey_data = $DB->get_record_sql("SELECT itus_create_date FROM mdl_itu_survey where id = ?", array($survey_id), $strictness=IGNORE_MISSING);
    //GET NEW DATA
    $titleSurvey = $fromform->txtSurveyTitle;
    $creator_id = $fromform->survey_creator_id;
    $program_id = $fromform->ddlProgram;
    //get majors in hidden fields and manipulate BEGIN
    $strMajors = $_POST["selected_major"];
    $major_ids = array();
    if(strlen($strMajors) == 0){
        //If majors is not selected, get all majors
        $sql = "select * from mdl_major where program_id = $program_id";
        $outputs = $DB->get_records_sql($sql);
        $count = 0;
        foreach($outputs as $record){
            $major_ids[$count] = $record->id;
            $count++;
        }    
    }else{
        $major_ids = explode(",", $strMajors);    
    }
    //get majors in hidden fields and manipulate END    
    //get questions in hidden fields and manipulate BEGIN
    $strQuestion = $_POST["selected_question"];    
    if(strlen($strQuestion) > 0){
		$question_ids = explode(",", $strQuestion);
	}else{
		$question_ids = array();
	}
    //get questions in hidden fields and manipulate END
    $depthLo =$fromform->ddlDepthLo;
    $expireDate =$fromform->expireDateTime;
    
    $titleOfSummary = $fromform->txtTitleOfSummary;
    $contentOfSummary = $fromform->txtAreaSummary['text'];
    $titleOfQuestion = $fromform->txtTitleOfQuestion;
    $contentOfQuestion = $fromform->txtAreaQuestion['text'];
    $titleOfLO = $fromform->txtTitleOfLO;
    $contentOfLo = $fromform->txtAreaLO['text'];
    
    $nowInt = time();
    $now = getdate($nowInt);
    //if expiredate has not set since the page load => add one week as default
    if(intval($expireDate) < $nowInt){
        $expireDate = $nowInt + ONE_WEEK;
    }
    
    $expiration = getdate($expireDate);
    //echo "expiration".$expiration["year"].'-'.$expiration["mon"].'-'.$expiration["mday"].' '.$expiration["hours"].':'.$expiration["minutes"].':'.$expiration["seconds"];
    $survey = array(
                "id"=>$survey_id,
                "itus_program"=>$program_id,
                "itus_creator"=>$creator_id,
                "itus_create_date"=>$old_survey_data->itus_create_date,
                "itus_update_date"=>$now["year"].'-'.$now["mon"].'-'.$now["mday"].' '.$now["hours"].':'.$now["minutes"].':'.$now["seconds"],
                "itus_title"=>$titleSurvey,
                "itus_expire_date"=>$expiration["year"].'-'.$expiration["mon"].'-'.$expiration["mday"].' '.$expiration["hours"].':'.$expiration["minutes"].':'.$expiration["seconds"],
                "itus_publish_date"=>$now["year"].'-'.$now["mon"].'-'.$now["mday"].' '.$now["hours"].':'.$now["minutes"].':'.$now["seconds"],
                "itus_depth_lo"=>$depthLo,
                "itus_summary_title"=>$titleOfSummary,
                "itus_summary"=>$contentOfSummary,
                "itus_question_title"=>$titleOfQuestion,
                "itus_question_summary"=>$contentOfQuestion,
                "itus_lo_title"=>$titleOfLO,
                "itus_lo_summary"=>$contentOfLo
              );
    
//    echo "title:".$titleSurvey."<br/>";
//    echo "creator:".$creator_id."<br/>";
//    echo "program:".$program_id."<br/>";
//    echo "majors:".$strMajors."<br/>";
//    echo "question:";
//    foreach($question_ids as $value){
//        echo $value."/ ";
//    }
//    echo "<br/>";
//    echo "depth:".$depthLo."<br/>";
//    echo "expiration:".$expiration["year"].'-'.$expiration["mon"].'-'.$expiration["mday"]."<br/>";
//    echo "titleSum:".$titleOfSummary."<br/>";
//    echo "contentSum:".$contentOfSummary."<br/>";
//    echo "titleQuestion:".$titleOfQuestion."<br/>";
//    echo "contentQuestion:".$contentOfQuestion."<br/>";
//    echo "titleLO:".$titleOfLO."<br/>";
//    echo "contentLO:".$contentOfLo."<br/>";
            
    if(isset($_POST["previewbtn"])){
        //If Preview
        
    }else if(isset($_POST["draftBtn"]))
    {
        //If draft
        //Clear Old Data
        $mylib->clearSurveyData($survey_id);
        //Replace New Data
        $mylib->edit_survey_and_draft($survey, $major_ids, $question_ids);
        echo "<script>redirectTo('surveymanagement.php')</script>";        
    }else if(isset($_POST["publishBtn"])){
        //If publish
        //Clear Old Data
        $mylib->clearSurveyData($survey_id);
        //Replace New Data
        $mylib->edit_survey_and_publish($survey, $major_ids, $question_ids);
        echo "<script>redirectTo('surveymanagement.php')</script>";        
    }else{
        //echo "<script>redirectTo('surveymanagement.php')</script>";
    }
}else{
    //check if id in url is correct (exist and editable)
    $sql = $DB->get_record_sql("select itus_expire_date,ISNULL(itus_publish_date) as editable from mdl_itu_survey where id = $survey_id");
    if($sql->editable==0){
        echo "<script>redirectTo('surveymanagement.php')</script>";
    }else if(!isset($sql->editable)){
        echo "<script>redirectTo('surveymanagement.php')</script>";
    }else{
        echo "<script>setDefaultValueToDateTimeSelectoMoodle('".$sql->itus_expire_date."','id_expireDateTime')</script>";
    }
}
echo $OUTPUT->footer();
?>